<template>
  <div class="search">
    <el-form :model="ruleForm" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="商品名称" prop="game">
        <el-input v-model="ruleForm.game"></el-input>
      </el-form-item>
      <el-form-item label="订单编号" prop="ord_num">
        <el-input v-model="ruleForm.ord_num"></el-input>
      </el-form-item>
      <el-form-item label="订单状态" prop="state">
        <el-select v-model="ruleForm.state" placeholder="请选择">
          <el-option label="未付款" value="0"></el-option>
          <el-option label="已付款" value="1"></el-option>
          <el-option label="退款中" value="-1"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="买家昵称" prop="username">
        <el-input v-model="ruleForm.username"></el-input>
      </el-form-item>
      <el-form-item label="下单时间">
        <el-col :span="11">
          <el-form-item prop="date1">
            <el-date-picker
              type="date"
              placeholder="选择开始时间"
              v-model="ruleForm.date1"
            ></el-date-picker>
          </el-form-item>
        </el-col>
        <el-col class="zhi" :span="1">至</el-col>
        <el-col :span="11">
          <el-form-item prop="date2">
            <el-date-picker
              type="date"
              placeholder="选择结束时间"
              v-model="ruleForm.date2"
            ></el-date-picker>
          </el-form-item>
        </el-col>
      </el-form-item>
      <el-form-item>
        <el-button @click="resetForm('ruleForm')">重置</el-button>
        <el-button type="primary" @click="search">搜索</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import { createNamespacedHelpers } from "vuex";
const { mapState, mapMutations, mapActions } = createNamespacedHelpers("order");
import { selectOrder } from "@/api/orderselect";
export default {
  data() {
    return {
      ruleForm: {
        game: "",
        ord_num: "",
        state: "",
        username: "",
        date1: "",
        date2: "",
      },
    };
  },
  methods: {
    ...mapMutations(["search_order", "set_tabNav"]),
    ...mapActions(["set_order"]),
    //搜索
    search() {
      let keys = Object.keys(this.ruleForm);
      let flag = keys.every((key) => {
        return this.ruleForm[key] === "";
      });
      if (flag) {
        this.$alert('请填写条件后筛选', '提示', {
          confirmButtonText: '确定'
        });
      } else {
        if (selectOrder(this.orderList, this.ruleForm).length===0) {
          this.search_order(selectOrder(this.orderList, this.ruleForm));
          this.$message({
          showClose: true,
          message: "未查询到订单",
          type: "warning",
        });
        } else {
          this.search_order(selectOrder(this.orderList, this.ruleForm));
        }
      }
    },
    //重置
    resetForm(formName) {
      this.$refs[formName].resetFields();
      this.set_order(this.tabNav);
    },
  },
  computed: {
    ...mapState({
      orderList: (state) => state.orderList,
      tabNav: (state) => state.tabNav,
    }),
  },
};
</script>

<style scoped>
.search {
  border-bottom: 1px solid lightgray;
}
.el-input {
  width: 220px;
}
.zhi {
  margin-left: 12px;
}
.el-form {
  display: flex;
  flex-wrap: wrap;
}
</style>
